// pages/search/search.js
import {
    HTTP
} from '../../utils/http-promise.js'
let http = new HTTP()
Page({

    /**
     * 页面的初始数据
     */
    data: {
        keyword: '',
        hotSearch: [],
        lesson: [],
        showLesson: false,
        total: 0,
        noData: false,
        historySearch: wx.getStorageSync('historySearch') || [],
        loading: false,
        noMore:false,
        timer:null
    },

    /**
     * 生命周期函数--监听页面加载
     */
    onLoad(options) {
        this.getHotSearch()
    },
    //取消按钮
    goBack(){
        wx.navigateBack()//返回1级
        // wx.navigateBack({
        //     delta:2//返回2级
        // })
    },
    //模糊查询
    onInput(event){
        clearTimeout(this.data.timer)
        this.data.timer = setTimeout(()=>{
            // this.onConfirm(event)
        },600)
    },
    //获取热门搜索
    getHotSearch() {
        http.request({
            url: '/hot_lesson'
        }).then((data) => {
            this.setData({
                hotSearch: data
            })
        })
    },
    //搜索
    onConfirm(e) {
        let keyword = e.detail.value || e.target.dataset.word;
        if (keyword) {
            this.addHistorySearch(keyword)
            this.searchBykeyword(keyword)
            this.setData({
                keyword: keyword
            })
        }
    },
    //搜索
    searchBykeyword(word) {
        this.locked()
        http.request({
            url: '/query' //?q=${word}&start=0&size=6
        }).then((data) => {
            setTimeout(() => {
                this.setLesson(data)
                this.unlocked()
            },500)
        },()=>{
            this.unlocked()
        })
    },
    //列表绑值
    setLesson(data, num) {
        this.data.total = data.total;
        let info = data.lesson
        if (num == 1) {
            info = this.data.lesson.concat(info)
        }
        if (data.total == 0) {
            this.setData({
                showLesson: true,
                noData: true
            })
        } else {
            this.setData({
                lesson: info,
                showLesson: true,
                noData: false
            })
        }
    },
    //搜索缓存
    addHistorySearch(value) {
        //获取的时候要用同步，否则获取不到
        let historySearch = wx.getStorageSync("historySearch") || [];
        if (!historySearch.includes(value)) {
            historySearch.unshift(value);
            if (historySearch.length > 10) {
                historySearch.pop()
            }
            wx.setStorage({
                key: 'historySearch',
                data: historySearch,
                success: () => {
                    this.setData({
                        historySearch: historySearch
                    })
                }
            })
        }
    },
    //清空搜索缓存
    rmHistory() {
        wx.removeStorageSync('historySearch')
        this.setData({
            historySearch: []
        })
    },
    //叉号
    onClear() {
        this.setData({
            keyword: '',
            lesson: [],
            showLesson: false,
            total: 0,
            noData: false,
            loading: false,
            noMore:false,
            timer:null
        })
    },
    /**
     * 生命周期函数--监听页面初次渲染完成
     */
    onReady() {

    },

    /**
     * 生命周期函数--监听页面显示
     */
    onShow() {

    },

    /**
     * 生命周期函数--监听页面隐藏
     */
    onHide() {

    },

    /**
     * 生命周期函数--监听页面卸载
     */
    onUnload() {

    },

    /**
     * 页面相关事件处理函数--监听用户下拉动作
     */
    onPullDownRefresh() {
        // 在.json文件里配置
        // "backgroundColor": "#eeeeee",    下拉时的背景颜色
        // "backgroundTextStyle": "light",  点的类型
        // "enablePullDownRefresh": true    开启下拉
        this.onClear()
    },
    /**
     * 页面上拉触底事件的处理函数
     */
    onReachBottom() {
        if(this.isLoading()){
            //加载更多的时候先判断是不是在加载中
            return
        }
        let word = this.data.keyword;
        if (this.data.lesson.length >= this.data.total) {
            this.setData({
                noMore:true
            })
            return
        } else {
            this.locked()
            http.request({
                url: '/query' //?q=${word}&start=0&size=6
            }).then((data) => {
                setTimeout(() => {
                    this.setLesson(data, 1)
                    this.unlocked()
                },1500)
            }, () => {
                this.unlocked()
            })
        }
    },
    //判断是否在加载中
    isLoading() {
        return this.data.loading ? true : false;
    },
    //上锁
    locked() {
        wx.showLoading({
            title:'正在加载'
        })
        // wx.showNavigationBarLoading()
        this.setData({
            loading: true
        })
    },
    //解锁
    unlocked() {
        wx.hideLoading();
        // wx.hideNavigationBarLoading()
        this.setData({
            loading: false
        })
    },
    /**
     * 用户点击右上角分享
     */
    onShareAppMessage() {

    }
})